Android: Is there a way to change MediaPlayer urls

2019-08-10 21:01发布

问题:

is there a way to change the urls without compiling the app or deployment once i pushed to the market? the url might change in future or point to different urls.

currently i am hardcoding the urls somethign like this:

 try {
 url = "http://ofertaweb.ro/android/sleepandlovemusic/" + songs_array[counter] + ".mp3";
 mediaPlayer.setDataSource(url);
 } 

回答1:

Do not hardcode your URL at project build time, consider writing code that dynamically resolve it at application run time. for example, you can create a static html page (contains a list of actual mp3 URLs), hardcode this static html page URL at project build time, every time your application starting running, query this static html page to get the up-to-date mp3 URL at application run time. there are many alternative way to achieve this, just give you some clue, hope this helps.



回答2:

here is what i found a way to read the html file:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class Get_Webpage {

    public String parsing_url = "";

    public Get_Webpage(String url_2_get){       
        parsing_url = url_2_get;
    }

    public String get_webpage_source(){

        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(parsing_url);
        HttpResponse response = null;
        try {
            response = client.execute(request);
        } catch (ClientProtocolException e) {

        } catch (IOException e) {

        }

        String html = "";
        InputStream in = null;
        try {
            in = response.getEntity().getContent();
        } catch (IllegalStateException e) {

        } catch (IOException e) {

        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder str = new StringBuilder();
        String line = null;
        try {
            while((line = reader.readLine()) != null)
            {
                str.append(line);
            }
        } catch (IOException e) {

        }
        try {
            in.close();
        } catch (IOException e) {

        }
        html = str.toString();

        return html;
    }

}

then you read like this:

try {
    Get_Webpage obj = new Get_Webpage("http://ofertaweb.ro/android/sleepandlovemusic/list_files.php");
    directory_listings = obj.get_webpage_source();
} catch (Exception e) {
    }

//Log.d("director listing", directory_listings);

songs_array = directory_listings.split(":::");