2013-04-25 7 views
0

assets/pages/id(id may be rice or sugar or meat ect)/id.mp4에 저장된 비디오를 실행하려고합니다. 이 응용 프로그램에서 어떤 항목 (rice or sugar or meat)의 비디오를 만지면 해당 mp4 비디오가 재생됩니다. 그러나 비디오가 제대로 재생되지 않습니다.android : 비디오가 재생되지 않습니다.

찾으려고하지만 실패합니다.

아래는 내 코드입니다

package com.app.teachmesushi; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.util.HashMap; 
import java.util.Map; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.content.ServiceConnection; 
import android.content.res.AssetManager; 
import android.graphics.PixelFormat; 
import android.media.MediaPlayer; 
import android.media.MediaPlayer.OnPreparedListener; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Environment; 
import android.os.Handler; 
import android.os.IBinder; 
import android.os.Message; 
import android.os.Messenger; 
import android.os.RemoteException; 
import android.util.Log; 
import android.widget.MediaController; 
import android.widget.VideoView; 

import com.flurry.android.FlurryAgent; 

public class VideoActivity extends Activity implements OnPreparedListener { 
private VideoView video; 
private MediaController ctlr; 
private String id; 
private File file; 
private ProgressDialog pd = null; 
private Integer msec = -1; 
private int start = 1; 

Messenger mService = null; 
boolean mIsBound; 
final Messenger mMessenger = new Messenger(new IncomingHandler()); 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getWindow().setFormat(PixelFormat.TRANSLUCENT); 
    setContentView(R.layout.activity_video); 

    final String id = getIntent().getExtras().getString("id"); 
    this.id = id; 

    if (savedInstanceState != null) { 
     msec = savedInstanceState.getInt("pos"); 
    } 

    video = (VideoView) findViewById(R.id.video); 
    ctlr = new MediaController(this, false); 
    ctlr.setMediaPlayer(video); 
    video.setMediaController(ctlr); 
    video.setOnPreparedListener(this); 

} 

@Override 
protected void onStart() { 
    super.onStart(); 
    FlurryAgent.onStartSession(this, MainActivity.FLURRY_KEY); 

    Map<String, String> articleParams = new HashMap<String, String>(); 
    articleParams.put("ID", id); // Capture user status 
    FlurryAgent.logEvent("Video", articleParams); 

    Log.e("sushi", "msec: " + msec); 
    Log.e("sushi", "start: " + start); 

    CheckIfServiceIsRunning(); 

    String fileName = id + ".mp4"; 
    file = new File(getExternalFilesDir(null), fileName); 

    if (file.exists()) { 
     video.setVideoPath(file.getPath()); 
    } else { 
     // Show the ProgressDialog on this thread 
     pd = ProgressDialog.show(VideoActivity.this, "Launching video", 
       "Accessing...", true, false); 
     pd.dismiss(); 

     // Start a new thread that will download all the data 
     new DownloadTask().execute(fileName); 
    } 

    if (msec != -1) { 
     video.seekTo(msec); 
    } else if (start == 1) { 
     start = 0; 
     video.start(); 
    } else if (msec == video.getDuration()) { 
     video.seekTo(0); 
    } 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    video.pause(); 
    msec = video.getCurrentPosition(); 
    pd.dismiss(); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    video.pause(); 
    msec = video.getCurrentPosition(); 

    doUnbindService(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 

    video = null; 
    ctlr = null; 
} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putInt("pos", video.getCurrentPosition()); 
} 

public void onPrepared(MediaPlayer mp) { 
    try { 
     video.requestFocus(); 
     ctlr.show(); 
    } catch (Exception e) { 

    } 
} 

class DownloadTask extends AsyncTask<String, Void, Object> { 
    protected Object doInBackground(String... args) { 
     AssetManager am = getAssets(); 
     String fileName = args[0]; 
     File file = new File(getExternalFilesDir(null), fileName); 
     Log.i("sushi", "Background thread starting"); 

     String state = Environment.getExternalStorageState(); 
     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      try { 
       InputStream in = am.open("pages/" + id + "/" + id + ".mp4"); 
       //InputStream in = am.open("http://176.9.35.93/tmc/videos/old/equipments.mp4"); 
       FileOutputStream f = new FileOutputStream(file); 
       byte[] buffer = new byte[1024]; 
       int len1 = 0; 
       while ((len1 = in.read(buffer)) > 0) { 
        f.write(buffer, 0, len1); 
       } 
       f.close(); 
       in.close(); 
      } catch (Exception e) { 
       Log.d("sushi", e.getMessage()); 
      } 

      if (VideoActivity.this.pd != null) { 
       VideoActivity.this.pd.dismiss(); 
       VideoActivity.this.pd = null; 
      } 
     } 

     return null; 
    } 

    protected void onPostExecute(Object result) { 
     Intent intent = new Intent(VideoActivity.this, VideoActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     Bundle b = new Bundle(); 
     b.putString("id", id); 
     intent.putExtras(b); 
     startActivity(intent); 
     finish(); 
    } 
} 

@Override 
public void onBackPressed() { 
    try { 
     Log.d("sushi", "Deleting file"); 
     file.delete(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     Log.e("sushi", "File delete failed"); 
    } 
    finish(); 
} 

private ServiceConnection mConnection = new ServiceConnection() { 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     mService = new Messenger(service); 
     try { 
      Message msg = Message.obtain(null, 
        TimerService.MSG_REGISTER_CLIENT); 
      msg.replyTo = mMessenger; 
      mService.send(msg); 
     } catch (RemoteException e) { 
      // In this case the service has crashed before we could even do 
      // anything with it 
     } 
    } 

    public void onServiceDisconnected(ComponentName className) { 
     // This is called when the connection with the service has been 
     // unexpectedly disconnected - process crashed. 
     mService = null; 
    } 
}; 

private void CheckIfServiceIsRunning() { 
    // If the service is running when the activity starts, we want to 
    // automatically bind to it. 
    if (TimerService.isRunning()) { 
     doBindService(); 
    } else { 
     Log.e("sushi", "Service not running"); 
    } 
} 

void doBindService() { 
    bindService(new Intent(this, TimerService.class), mConnection, 
      Context.BIND_AUTO_CREATE); 
    mIsBound = true; 
    Log.e("sushi", "Bound to service"); 
} 

void doUnbindService() { 
    if (mIsBound) { 
     // If we have received the service, and hence registered with it, 
     // then now is the time to unregister. 
     if (mService != null) { 
      try { 
       Message msg = Message.obtain(null, 
         TimerService.MSG_UNREGISTER_CLIENT); 
       msg.replyTo = mMessenger; 
       mService.send(msg); 
      } catch (RemoteException e) { 
       // There is nothing special we need to do if the service has 
       // crashed. 
      } 
     } 
     // Detach our existing connection. 
     unbindService(mConnection); 
     mIsBound = false; 
    } 
} 

class IncomingHandler extends Handler { 
    @Override 
    public void handleMessage(Message msg) { 
     switch (msg.what) { 
     case TimerService.MSG_SET_INT_VALUE: 
      Log.e("sushi", String.valueOf(msg.arg1)); 
      if (msg.arg1 <= 1) { 
       video.pause(); 
      } 

      break; 
     default: 
      super.handleMessage(msg); 
     } 
    } 
} 
} 

내 로그 캣 오류가

04-25 21:15:57.371: E/sushi(273): msec: -1 
04-25 21:15:57.371: E/sushi(273): start: 1 
04-25 21:15:57.381: E/sushi(273): Service not running 
04-25 21:15:58.121: E/sushi(273): msec: -1 
04-25 21:15:58.121: E/sushi(273): start: 1 
04-25 21:15:58.121: E/sushi(273): Service not running 

Pls는이

답변

0

모든 것이 ok입니다 도움이됩니다. 문제는 실제 장치가 아니라 에뮬레이터에서이 서비스를 실행한다는 것입니다. 실제 장치에서 테스트 할 때 제대로 실행됩니다. 하지만 에뮬레이터의 문제.

관련 문제