0

我正在尝试运行存储在assets/pages/id(id may be rice or sugar or meat ect)/id.mp4. 在这个应用程序中,如果我触摸任何项目 ( rice or sugar or meat) 的视频,它将播放相应的 mp4 视频。但视频播放不正常。

试图找出但失败了。

下面是我的代码

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);
        }
    }
}
}

我的 Logcat 错误是

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

请帮忙

4

1 回答 1

0

一切都是ok。问题是我在模拟器中而不是在真实设备中运行此服务。当我在真实设备中测试时,它运行正常。但是模拟器中的问题。

于 2013-04-27T07:01:25.990 回答