android - 从android中的url流式传输音频/视频文件

标签 android streaming

我想从 url 流式传输音频(mp3)/视频(mp4)文件,还想设置搜索栏,以便用户可以按时间搜索歌曲。如果用户搜索歌曲,则应该从该歌曲开始播放。我能够从 url 流式传输和播放文件,但无法添加搜索栏来实现查看功能。任何示例或任何帮助。

提前致谢

最佳答案

我已经创建了一个演示项目,我认为它会对你有所帮助

这是 Activity 课

public class VideoViewDemoProj extends Activity {
    private static final String TAG = "VideoViewDemo";

    private VideoView mVideoView;
    private EditText mPath;
    private ImageButton mPlay;
    private ImageButton mPause;
    private ImageButton mReset;
    private ImageButton mStop;
    private String current;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        mVideoView = (VideoView) findViewById(R.id.surface_view);

        mPath = (EditText) findViewById(R.id.path);
    //  mPath.setText("http://logisticinfotech.com/extra/Veer.mp4");
    //  mPath.setText("http://www.youtube.com/watch?v=rkOySwlEtVk&feature=youtube_gdata_player");
        mPath.setText("http://logisticinfotech.com/client/full-volume.mp4");
        mPlay = (ImageButton) findViewById(R.id.play);
        mPause = (ImageButton) findViewById(R.id.pause);
        mReset = (ImageButton) findViewById(R.id.reset);
        mStop = (ImageButton) findViewById(R.id.stop);

        mPlay.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                playVideo();
            }
        });
        mPause.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                if (mVideoView != null) {
                    mVideoView.pause();
                }
            }
        });
        mReset.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                if (mVideoView != null) {
                    mVideoView.seekTo(0);
                }
            }
        });
        mStop.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                if (mVideoView != null) {
                    current = null;
                    mVideoView.stopPlayback();
                }
            }
        });
        runOnUiThread(new Runnable(){
            public void run() {
                playVideo();

            }

        });
    }

    private void playVideo() {
        try {
            final String path = mPath.getText().toString();
            System.out.println("path --> "+path);
            Log.v(TAG, "path: " + path);
            if (path == null || path.length() == 0) {
                Toast.makeText(VideoViewDemoProj.this, "File URL/path is empty",
                        Toast.LENGTH_LONG).show();

            } else {
                // If the path has not changed, just start the media player
                if (path.equals(current) && mVideoView != null) {
                    mVideoView.start();
                    mVideoView.requestFocus();
                    return;
                }
                current = path;
                System.out.println("Current path --> "+path);
                mVideoView.setVideoPath(getDataSource(path));
                mVideoView.start();
                mVideoView.requestFocus();

                System.out.println("end try in play");

            } 
        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
            if (mVideoView != null) {
                mVideoView.stopPlayback();
            }
        }
    }

    private String getDataSource(String path) throws IOException {
        if (!URLUtil.isNetworkUrl(path)) {
            return path;
        } else {
            URL url = new URL(path);
            URLConnection cn = url.openConnection();
            cn.connect();
            InputStream stream = cn.getInputStream();
            if (stream == null)
                throw new RuntimeException("stream is null");
            File temp = File.createTempFile("mediaplayertmp", "mp4");
            System.out.println("hi");
            temp.deleteOnExit();
            String tempPath = temp.getAbsolutePath();

            FileOutputStream out = new FileOutputStream(temp);
            byte buf[] = new byte[128];
            do {
                int numread = stream.read(buf);
                if (numread <= 0)
                    break;
                out.write(buf, 0, numread);
            } while (true);
            try {
                stream.close();
            } catch (IOException ex) {
                Log.e(TAG, "error: " + ex.getMessage(), ex);
            }
            return tempPath;
        }
    }
}

XML 文件如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <EditText android:id="@+id/path"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
            />
    <VideoView android:id="@+id/surface_view"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent">
    </VideoView>
    <LinearLayout
            android:orientation="horizontal"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            >
        <ImageButton android:id="@+id/play"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/play"/>

        <ImageButton android:id="@+id/pause"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/pause"/>
        <ImageButton android:id="@+id/reset"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/reset"/>
        <ImageButton android:id="@+id/stop"
                     android:layout_height="wrap_content"
                     android:layout_width="wrap_content"
                     android:src="@drawable/stop"/>
    </LinearLayout>
</LinearLayout>

在AndroidManifest.xml文件中添加以下权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

关于android - 从android中的url流式传输音频/视频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7453967/

相关文章:

android - 为 Android 开发流媒体服务器

ios - AVPlayer seekTo 不准确

android - 获取本地网络(LAN)中的设备列表?

android - ListView - ImageLoader 在向上滚动时移动列表/项目

Android镜像矢量可绘制

java - RecyclerView 设置最后一个ArrayList项数据为第一项

java - 在主要 Activity 中膨胀 fragment 会带来错误

javascript - 通过 Node.js 和 Socket.io 流式传输音频/视频

ffmpeg - 减少 HLS 流式传输 FFMPEG 的延迟

Java 文件未写入带有换行符的流