Sagar Pawar
Sagar Pawar

Reputation: 3

Why does my media player stops unfortunately?

This is the complete code of my media-player, what it does is, it reads the file from the URL, stores them to an array and then starts playing from location ZERO of the array (song_url). I guess there is a problem with my emulator, is it so? Please help me, Thank you in advance.

package com.hiphop.streamingmediaplayer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class Jsonmedia extends Activity {
private MediaPlayer mp;
Button play;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jsonview);

    mp = new MediaPlayer();
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

    play = (Button) findViewById(R.id.play);

    play.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
                // Create a URL for the desired page
                URL url = new URL(
                        "http://reallifethug.webs.com/temp_list.txt");
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(url.openStream()));

                int ch;
                int EOF = -1; // end of file
                String list[];

                String str = "";
                while ((ch = in.read()) != EOF) {
                    str += (char) ch;
                }
                in.close();

                list = str.split("\n");

                for (ch = 0; ch < list.length; ++ch)
                    list[ch] += "\n";

                String song_name[] = new String[list.length];
                String song_url[] = new String[list.length];

                for (int i = 0; i < list.length; i++) {
                    song_name[i] = "";
                    song_url[i] = "";
                    int a = 0;
                    char c;

                    while ((c = list[i].charAt(a++)) != ' ')
                        song_name[i] += c;

                    while ((c = list[i].charAt(a++)) != '\n')
                        song_url[i] += c;

                    System.out.println(song_name[i]);
                    System.out.println(song_url[i]);
                }
                mp.setDataSource(song_url[0]);
                mp.prepare();
                mp.start();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.jsonmedia, menu);
    return true;

}

}

These are my ERRORS after the application STARTS

05-16 12:42:20.918: I/Process(1832): Sending signal. PID: 1832 SIG: 9
05-16 12:42:21.678: E/Trace(1882): error opening trace file: No such file or directory (2)
05-16 12:42:22.228: I/Choreographer(1882): Skipped 73 frames!  The application may be doing to o much work on its main thread.
05-16 12:42:22.288: D/gralloc_goldfish(1882): Emulator without GPU emulation detected.

These are my ERRORS after i click on Play button, the app "unfortunately stops"

05-16 12:46:00.698: D/AndroidRuntime(1882): Shutting down VM
05-16 12:46:00.698: W/dalvikvm(1882): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-16 12:46:00.758: E/AndroidRuntime(1882): FATAL EXCEPTION: main
05-16 12:46:00.758: E/AndroidRuntime(1882): android.os.NetworkOnMainThreadException
05-16 12:46:00.758: E/AndroidRuntime(1882):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at  java.net.InetAddress.getAllByName(InetAddress.java:214)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at java.net.URL.openStream(URL.java:462)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at com.hiphop.streamingmediaplayer.Jsonmedia$1.onClick(Jsonmedia.java:38)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at android.view.View.performClick(View.java:4204)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at android.view.View$PerformClick.run(View.java:17355)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at android.os.Handler.handleCallback(Handler.java:725)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at android.os.Looper.loop(Looper.java:137)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at java.lang.reflect.Method.invokeNative(Native Method)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at java.lang.reflect.Method.invoke(Method.java:511)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-16 12:46:00.758: E/AndroidRuntime(1882):     at dalvik.system.NativeStart.main(Native Method)
05-16 12:46:04.348: I/Process(1882): Sending signal. PID: 1882 SIG: 9

Upvotes: 0

Views: 248

Answers (4)

Peshal
Peshal

Reputation: 1518

Here is a workaround,

    StrictMode.ThreadPolicy ourPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(ourPolicy);

add this in the onCreate() method

Upvotes: 0

Sander van&#39;t Veer
Sander van&#39;t Veer

Reputation: 5980

Looks like you're trying to run a network request on the main UI thread. Android does not allow you to do that since 3.0 (I believe). Doing so causes your UI to lock up until the request is completed, rendering your app useless during the execution of the request.

You'll either have to run your request in a new Thread or an ASyncTask, to take the load of the UI thread. You can find more info on how to use multiple threads here.

Upvotes: 1

Crossle Song
Crossle Song

Reputation: 10174

Don't operate the network on MainThread.

Upvotes: 1

Michael Schmidt
Michael Schmidt

Reputation: 391

You get a NetworkOnMainThreadException. It is thrown because it is not allowed to do network operations in the main thread of your application.

First hit on google about this exception: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

Second hit on google about this exception: How to fix android.os.NetworkOnMainThreadException?

Upvotes: 1

Related Questions