主文件

package cn.com.sxp;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class SurfaceView4VideoActivity extends Activity implements
View.OnClickListener {
// 下一步要实现的
// 1. 活动onResume之后继续以前播放点播放
// 2. 重新运行程序时,要能从上一次运行播放的时间点开始播放




// ImageButton:
// Displays a button with an image (instead of text 展示的是图片而不是文本) that can be
// pressed or clicked by the user. By default, an ImageButton looks like a
// regular Button看起来像常规的按钮, with the standard button background that changes
// color during different button states. The image on the surface of the
// button
// is defined either by the android:src attribute in the XML element or by
// the setImageResource(int) method. 图片来源要么来自于xml文件,要么来自方法
// To remove the standard button background image, define your own
// background image or set the background color to be transparent.
// To indicate the different button states (focused, selected, etc.), you
// can define a different image for each state.可以为按钮每一个不同状态选择不同的图片
// E.g., a blue image by default, an orange one for when focused, and a
// yellow one for when pressed. An easy way to do this is with an XML
// drawable
// 提供一种图片源的方法
// "selector." For example:
// <?xml version="1.0" encoding="utf-8"?>
// <selector xmlns:andro>
}
}
}

 

主文件代码不多,但是有很多地方值得研究,我喜欢这么几个地方:

1. stop.setOnClickListener(this);

    该方法就是以一个接口为参数,官方叫注册。这个接口就是实现View.OnClickListener几个破方法,而本类刚好实现了,所以本类就可以作为这个接口参数。注册什么呀,说白了,就是指定一个人,当这个按钮被点击了,这个人实现的方法就能响应这个点击动作。这个人就是本类了;

2. sv.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 真他妈的不理解,后来上网查了,大概意思是说SurfaceView本身不包含原生数据,其数据来源于其它对象,比如Camera预览,就是由Camera对象向SurfaceView提供数据。 还算好理解;

3. sv.getHolder().addCallback(new Callback() ; 这个灰常的经典。surfaceHolder说是控制管理surfaceView,但它拿什么控制?屎吗?还不是靠着什么surfaceDestroyed几个烂方法吗。这几个方法谁实现?显然是new Callback()这么个隐含的类实现。与第1点相同,这个类要被注册, addCallback就干的这勾当。

4. 精华部分来了,那就是播放器的使用方法了,一个个来:

   1) reset方法。英文解释如下:

        Resets the MediaPlayer to its uninitialized state. After calling this method, you will have to initialize it again by setting the data source and calling prepare(). 官方的意思就是,调用这个方法后,状态就像恢复到出厂设置一样,要1. 重新设置数据源;2. 调用prepare方法,就这两屁件事;

   2)setAudioStreamType方法。英文解释:

        Sets the audio stream type for this MediaPlayer. See AudioManager for a list of stream types. Must call this method before prepare() or prepareAsync() in order for the target stream type to become effective thereafter.Parameters streamtype the audio stream type

        这个方法必须得在prepare方法之前调用,因为要生效

        AudioManager: AudioManager provides access to volume and ringer振铃 mode control.STREAM_MUSIC : The audio stream for music playback播放

   3) 要播放的音乐或者视频路径,还可以是http网上的某个音乐和视频,牛叉~~

   4)  setDisplay方法:把视频画面输出到SurfaceView。Sets the SurfaceHolder to use for displaying the video portion of the media. Either a surface holder or surface must be set if a display or video sink is needed. Not calling this method or setSurface(Surface) when playing back a video will result in only the audio track being played. A null surface holder or surface will result in only the audio track being played.    // 这个方法是用来在surfaceView上显示视频的,不调用的话就只有音频数据;

   5) prepare方法:Prepares the player for playback, synchronously同步. After setting the datasource and the display surface 这句话很重要, you need to either call prepare() or prepareAsync(). For files, it is OK to call prepare(), which blocks until MediaPlayer is ready for playback. 

   6)start方法,这个很简单了,Starts or resumes playback.开始或者恢复播放. If playback had previously been paused, playback will continue from where it was paused. If playback had been stopped, or never started before, playback will start at the beginning. 是吗?要测试~~

神了,接下来要测试的几个地方:

1. 在播放状态时,再按下播放按钮,应该从开头重新播放;

2. 在播放状态,按下暂停,应该是暂停;再按下暂停,应该从暂停处开始播放;

3. 在暂停状态下,按下播放,应该是从暂停处播放,而不是从开头播放;

    在暂停状态下,按下停止,应该就停止;

4. 在停止的状态下,按下开始,应该从头开始播放;

    在停止状态下,按下暂停,应该从头开始播放;

5. 注释掉position = mp.getCurrentPosition();看看恢复播放时从哪里播放;

昨天上传到sdcard卡的视频,今天在运行发现没了。今天再上传时,视频上传不了,而且无法创建目录。只有重新建立新的sdcard卡。

xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:orientation
="vertical" >

<SurfaceView
android:id="@+id/sv"
android:layout_width
="fill_parent"
android:layout_height
="300px" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:gravity
="center_horizontal"
android:orientation
="horizontal" >

<ImageButton
android:id="@+id/play"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:src
="@drawable/ic_launcher" />

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

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

</LinearLayout>

xml文件就没有什么好分析的,希望谷歌哥尽早出个好用的界面设计工具,那么戳的ADT简直无法忍受,但是比起我国来,还是很牛逼的~~~

相关文章:

  • 2022-12-23
  • 2021-12-23
  • 2022-03-08
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
  • 2021-12-06
猜你喜欢
  • 2022-12-23
  • 2022-02-14
  • 2021-11-14
  • 2021-08-08
  • 2021-08-22
  • 2022-12-23
相关资源
相似解决方案